#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QFile>
#include <QMessageBox>
#include <QScreen>
#include <QToolTip>
#include <QPixmap>
#include <QList>
#include <vector>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->shangxia,SIGNAL(triggered()),this,SLOT(on_shangxia_clicked()));
    connect(ui->zuoyou,SIGNAL(triggered()),this,SLOT(on_zuoyou_clicked()));
    connect(ui->lunkuo,SIGNAL(triggered()),this,SLOT(on_lunkuo_clicked()));
    connect(ui->ruihua,SIGNAL(triggered()),this,SLOT(on_ruihua_clicked()));
    connect(ui->erzhihua,SIGNAL(triggered()),this,SLOT(on_erzhihua_clicked()));
    connect(ui->open,SIGNAL(triggered()),this,SLOT(on_open_clicked()));
    connect(ui->pinghua,SIGNAL(triggered()),this,SLOT(on_pinghua_clicked()));

}

void MainWindow::on_pinghua_clicked()
{
    qDebug() << "gaussian_smoothing is triggered";
   double sigma=0.251;
       //原图的图片信息
       QPixmap  pix = QPixmap(ui->label_3->pixmap());
       QImage img  = pix.toImage();
       double imgWidth = ui->label_3->pixmap().width();
       double imgHeight = ui->label_3->pixmap().height();
       QPixmap newPix = QPixmap(imgWidth,imgHeight);
       QImage newImg = newPix.toImage();

       std::vector<int> tempRed;
       std::vector<int> tempGreen;
       std::vector<int> tempBlue;

       int size = 3;
       double dGaussianTemplate[3][11];
       void generate_gaussian_template(double gTemp[][11], int kSize, double sigma);
       generate_gaussian_template(dGaussianTemplate,size,sigma);
       int *gaussianTemplate = new int[size*size];
       int templateSum = 0;
       for(int i=0;i<size;i++)
       {
           for(int j=0;j<size;j++)
           {
               gaussianTemplate[i+j*size] = floor(dGaussianTemplate[i][j]);
               templateSum += floor(dGaussianTemplate[i][j]);
           }
       }

       int newRed = 0;
       int newGreen = 0;
       int newBlue = 0;
       int i,j,m,n,k;
       for(i=0; i<imgWidth;i++)
           for(j=0;j<imgHeight;j++)
           {
               if(i!=0 && j!= 0 && (i != imgWidth-1) && (j!=imgHeight-1))
               {
                   // 处理红色通道
                   for(m=i-1;m<i+2;m++)
                        for(n=j-1;n<j+2;n++)
                           tempRed.push_back(img.pixelColor(m,n).red());

                   newRed = 0;
                   for(k =0;k<9;k++)
                   {
                       newRed += tempRed[k] * gaussianTemplate[k];
                   }
                   newRed =  newRed / templateSum;

                   // 处理绿色通道
                   for(m=i-1;m<i+2;m++)
                        for(n=j-1;n<j+2;n++)
                           tempGreen.push_back(img.pixelColor(m,n).green());

                   newGreen = 0;
                   for(int k=0;k<9;k++)
                   {
                       newGreen += tempGreen[k] * gaussianTemplate[k];
                   }
                   newGreen =  newGreen / templateSum;

                   // 处理蓝色通道
                   for(m=i-1;m<i+2;m++)
                        for(n=j-1;n<j+2;n++)
                           tempBlue.push_back(img.pixelColor(m,n).blue());

                   newBlue = 0;
                   for(k =0;k<9;k++)
                   {
                       newBlue += tempBlue[k] * gaussianTemplate[k];
                   }
                   newBlue =  newBlue/ templateSum;

                   newImg.setPixelColor(i,j,QColor(newRed,newGreen,newBlue));
                   tempRed.clear();
                   tempGreen.clear();
                   tempBlue.clear();
               }else
               {
                  //处理边缘
                   newImg.setPixelColor(i,j,img.pixelColor(i,j));
               }
           }

       //重新布局界面大小，并显示处理后的图像
  //     set_canva_size(newImg.width(),newImg.height());
       ui->label_4->setPixmap(QPixmap::fromImage(newImg));

}

void generate_gaussian_template(double gTemp[][11], int kSize, double sigma)
{
    //static const double PI = 3.1415926535;
    //模板的中心位置
    int center = kSize / 2;
    double x2, y2;
    for(int i = 0; i<kSize; i++)
    {
        x2 = pow(i-center,2);
        for(int j =0; j<kSize;j++)
        {
            y2 = pow(j-center,2);
            double g = exp(-(x2+y2) / (2*sigma*sigma));
            g /= 2* 3.1415926535 * sigma;
            gTemp[i][j] = g;
        }
    }
    //将右上角系数进行归一化处理
    double t = 1 /gTemp[0][0];
    for(int i =0;i<kSize;i++)
        for(int j=0; j<kSize;j++)
            gTemp[i][j] *=t;
}

QImage Binaryzation(const QImage &origin)
{
    int width = origin.width();
    int height = origin.height();
    QImage newImg = QImage(width, height, QImage::Format_RGB888);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int gray = qGray(origin.pixel(x, y));
            int newGray;
            if (gray > 128)
                newGray = 250;
            else
                newGray = 0;
            newImg.setPixel(x, y, qRgb(newGray, newGray, newGray));
        }
    }
    return newImg;
}//二值化函数
void MainWindow::on_erzhihua_clicked()
{
    ui->label_4->setScaledContents(true);
    ui->label_4->setPixmap(QPixmap::fromImage(Binaryzation(image)));
}//二值化槽操作函数

QImage Vertical(const QImage &origin)
{
    QImage newImage(QSize(origin.width(), origin.height()), QImage::Format_ARGB32);
    QColor tmpColor;
    int r, g, b;
    for (int x = 0; x < newImage.width(); x++) {
        for (int y = 0; y < newImage.height(); y++) {
            tmpColor = QColor(origin.pixel(x, y));
            r = tmpColor.red();
            g = tmpColor.green();
            b = tmpColor.blue();

            newImage.setPixel(x, newImage.height() - y - 1, qRgb(r, g, b));

        }
    }
    return newImage;
}//上下翻转函数
void MainWindow::on_shangxia_clicked()
{
    ui->label_4->setScaledContents(true);
    ui->label_4->setPixmap(QPixmap::fromImage(Vertical(image)));
}//上下翻转槽操作函数

QImage Horizontal(const QImage &origin)
{
    QImage newImage(QSize(origin.width(), origin.height()), QImage::Format_ARGB32);
    QColor tmpColor;
    int r, g, b;
    for (int x = 0; x < newImage.width(); x++) {
        for (int y = 0; y < newImage.height(); y++) {
            tmpColor = QColor(origin.pixel(x, y));
            r = tmpColor.red();
            g = tmpColor.green();
            b = tmpColor.blue();

            newImage.setPixel(newImage.width() - x - 1, y, qRgb(r, g, b));

        }
    }
    return newImage;
}//左右翻转函数
void MainWindow::on_zuoyou_clicked()
{
    ui->label_4->setScaledContents(true);
    ui->label_4->setPixmap(QPixmap::fromImage(Horizontal(image)));
}//左右翻转槽操作函数

QImage ContourExtraction(const QImage &img)
{
    int width = img.width();
    int height = img.height();
    int pixel[8];
    QImage binImg = Binaryzation(img);
    QImage newImg = QImage(width, height, QImage::Format_RGB888);
    newImg.fill(Qt::white);

    uint8_t *rgb = newImg.bits();
    uint8_t *binrgb = binImg.bits();
    int nRowBytes = (width * 24 + 31) / 32 * 4;
    int  lineNum_24 = 0;
    for (int y = 1; y < height - 1; y++) {
        for (int x = 1; x < width - 1; x++) {
            memset(pixel, 0, 8);
            lineNum_24 = y * nRowBytes;
            if (binrgb[lineNum_24 + x * 3] == 0) {
                rgb[lineNum_24 + x * 3] = 0;
                rgb[lineNum_24 + x * 3 + 1] = 0;
                rgb[lineNum_24 + x * 3 + 2] = 0;
                pixel[0] = binrgb[(y - 1) * nRowBytes + (x - 1) * 3];
                pixel[1] = binrgb[(y) * nRowBytes + (x - 1) * 3];
                pixel[2] = binrgb[(y + 1) * nRowBytes + (x - 1) * 3];
                pixel[3] = binrgb[(y - 1) * nRowBytes + (x) * 3];
                pixel[4] = binrgb[(y + 1) * nRowBytes + (x) * 3];
                pixel[5] = binrgb[(y - 1) * nRowBytes + (x + 1) * 3];
                pixel[6] = binrgb[(y) * nRowBytes + (x + 1) * 3];
                pixel[7] = binrgb[(y + 1) * nRowBytes + (x + 1) * 3];

                if (pixel[0] + pixel[1] + pixel[2] + pixel[3] + pixel[4] + pixel[5] + pixel[6] + pixel[7] == 0) {
                    rgb[lineNum_24 + x * 3] = 255;
                    rgb[lineNum_24 + x * 3 + 1] = 255;
                    rgb[lineNum_24 + x * 3 + 2] = 255;
                }

            }
        }
    }
    return newImg;
}//轮廓提取
void MainWindow::on_lunkuo_clicked()
{
    ui->label_4->setScaledContents(true);
    ui->label_4->setPixmap(QPixmap::fromImage(ContourExtraction(image)));
}//轮廓提取槽操作函数


QImage* Sharpen(QImage* image)
{
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
    int kernel [3][3] = {
    {-1,-1,-1},
    {-1,8,-1},
    {-1,-1,-1}};
    int sizeKernel = 3;
    int sumKernel = 3;
    QColor color;
     for(int x = sizeKernel/2;x<image->width() - sizeKernel/2;x++)
    {
       for(int y= sizeKernel/2;y<image->height() - sizeKernel/2;y++)
        {
            int r = 0;
            int g = 0;
            int b = 0;
            for(int i = -sizeKernel/2;i<=sizeKernel/2;i++)
            {
               for(int j = -sizeKernel/2;j<=sizeKernel/2;j++)
                {
                 color = QColor(image->pixel(x+i,y+j));
                 r+=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 g+=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                 b+=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                }
            }
            r = r/sumKernel+ color.red();
            g = g/sumKernel+ color.green();
            b = b/sumKernel + color.blue();
            r = qBound(0,r,255) ;
            g = qBound(0,g,255) ;
            b = qBound(0,b,255) ;
            newImage->setPixel(x,y,qRgb( r,g,b));
        }
    }
    return newImage;
}

void MainWindow::on_ruihua_clicked()
{
    ui->label_4->setScaledContents(true);
    ui->label_4->setPixmap(QPixmap::fromImage(*Sharpen(&image)));
}

void MainWindow::on_open_clicked()
{
    QString OpenFile, OpenFilePath;
            OpenFile = QFileDialog::getOpenFileName(this,"Please choose an image file","","Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
            if( OpenFile != "" )
            {
                if( image.load(OpenFile) )
                {
                    ui->label_3->setScaledContents(true);
                    ui->label_3->setPixmap(QPixmap::fromImage(image));
                }
            }
}


MainWindow::~MainWindow()
{
    delete ui;
}




